import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:image_picker/image_picker.dart';
import 'package:magic_car/UI/Loading.dart';
import 'package:magic_car/common/PrimayButton.dart';
import 'package:magic_car/common/nodata.dart';
import 'package:magic_car/common/showbottomsheet.dart';
import 'package:magic_car/user/anserdetail.dart';
import 'package:magic_car/user/checkstudyinfo.dart';
import 'package:magic_car/user/mytask.dart';
import 'package:magic_car/user/pickvideo.dart';
import 'package:magic_car/user/taskdetail.dart';
import 'package:magic_car/user/testvideo.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
// import 'package:flutter_file_preview/flutter_file_preview.dart';

class TaskUnionOrderPage extends StatefulWidget {
  TaskUnionOrderPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<TaskUnionOrderPage> {
  List formList;
  List orders;
  ScrollController _controller = new ScrollController();
  int page = 1;
  bool end = false;
  String keyword = "";
  var imgsLeng = 0;
  String current_pdf_url;
  File videos;
  bool _saving = false;
  initState() {
    super.initState();
    _controller.addListener(() {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        print('滑动到了最底部');
        loadData();
      }
    });

    formList = [];
    loadData();
  }

  //加载数据
  loadData() async {
    // if(keyword!=""){
    //   print(keyword);
    // if(end)return;
    // }
    if (end) return;
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["uid"] = pref.getString('uid');
    map["page"] = page.toString();
    map["keyword"] = keyword;
    print(map);

    HttpHelper.post(HttpHelper.API_TaskOrders, (data) async {
      if (data != null) {
        print(data);
        print(data["info"].length.toString());
        if (data["info"].length == 0) {
          Toast.show('暂无更多数据', context);
        } else {
          if (data["code"].toString() == "1") {
            if (data["info"].length < 10) {
              setState(() {
                end = true;
              });
            }
            if (page > 1) {
              List add = data["info"];
              orders.addAll(add);
              setState(() {
                orders = orders;
              });
            } else {
              setState(() {
                orders = data["info"];
                page = page + 1;
              });
            }
          }
        }
      }
    }, params: map);
  }

  Widget buildImgs(item) {
    List<Widget> tiles = [];

    Widget content;

    List items = item["image_url"];
    print(items.length.toString());

    for (var i = 0; i < items.length; i++) {
      //List temp1 = items[i] as List;
      tiles.add(Padding(
        padding: new EdgeInsets.fromLTRB(5, 0, 0, 10),
        child: GestureDetector(
            onTap: () {
              // print('点击查看图片');
              // Navigator.of(context).push(
              //   NinePicture(items, i),
              // );
            },
            child: Image.network(items[i], width: 100, height: 100)),
      ));
    }
    content = new Wrap(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }
  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;
    if (orders.length == 0) {
      tiles.add(
          //Nodata()
          Center(
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
            Padding(
                padding: new EdgeInsets.fromLTRB(
                    15, MediaQuery.of(context).size.height / 2 - 100, 15, 0),
                child: new Text('暂无订单数据'))
          ])));
    } else {
      for (var i = 0; i < orders.length; i++) {
        var order = orders[i];
        tiles.add(Padding(
            padding: new EdgeInsets.fromLTRB(15, 10, 15, 0),
            child: Container(
              decoration: new BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
              ),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 2,
                            child: new Text(
                              order["order_sn"].toString(),
                              style: TextStyle(
                                  color: Color.fromRGBO(51, 51, 51, 1),
                                  fontSize: 13),
                            )),
                        Expanded(
                          flex: 1,
                          child: new Align(
                              alignment: Alignment.centerRight,
                              child: Container(
                                  padding: new EdgeInsets.fromLTRB(5, 2, 5, 2),
                                  color: Color.fromRGBO(27, 182, 251, 0.3),
                                  child: new Text(
                                    order["test_type"],
                                    style: TextStyle(
                                        color: Color.fromRGBO(27, 182, 251, 1),
                                        fontSize: 12),
                                  ))),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: new Text(
                              '联系电话',
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Color.fromRGBO(51, 51, 51, 1)),
                            )),
                        Expanded(
                          flex: 1,
                          child: new Align(
                              alignment: Alignment.centerRight,
                              child: Container(
                                  child: new Text(
                                order["phone_num"].toString(),
                                style: TextStyle(
                                    color: Color.fromRGBO(153, 153, 153, 1),
                                    fontSize: 12),
                              ))),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: new Text(
                              '检测完成时间',
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Color.fromRGBO(51, 51, 51, 1)),
                            )),
                        Expanded(
                          flex: 1,
                          child: new Align(
                              alignment: Alignment.centerRight,
                              child: Container(
                                  child: new Text(
                                order['overtime'],
                                style: TextStyle(
                                    color: Color.fromRGBO(153, 153, 153, 1),
                                    fontSize: 12),
                              ))),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: new Text(
                              '订单金额',
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Color.fromRGBO(51, 51, 51, 1)),
                            )),
                        Expanded(
                          flex: 1,
                          child: new Align(
                              alignment: Alignment.centerRight,
                              child: Container(
                                  child: new Text(
                                '¥' + order['orderPrice'].toString(),
                                style: TextStyle(
                                    color: Color.fromRGBO(251, 73, 57, 1),
                                    fontSize: 12),
                              ))),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: new Text(
                              '汽车品牌',
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Color.fromRGBO(51, 51, 51, 1)),
                            )),
                        Expanded(
                          flex: 1,
                          child: new Align(
                              alignment: Alignment.centerRight,
                              child: Container(
                                  child: new Text(
                                order["brand_id"].toString()!= "0"
                                    ? order["brand_string"]["name"]
                                    : '暂无',
                                style: TextStyle(
                                    color: Color.fromRGBO(153, 153, 153, 1),
                                    fontSize: 12),
                              ))),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
                    child: Align(
                        alignment: Alignment.centerRight,
                        child: new Container(
                          child: new Material(
                            //INK可以实现装饰容器
                            child: new Ink(
                              //用ink圆角矩形
                              // color: Colors.red,
                              decoration: new BoxDecoration(
                                //背景
                                color: Colors.white,
                                //设置四周圆角 角度
                                borderRadius:
                                    BorderRadius.all(Radius.circular(15.0)),
                                //设置四周边框
                                border: new Border.all(
                                    width: 1,
                                    color: Color.fromRGBO(27, 182, 251, 1)),
                              ),
                              child: new InkWell(
                                  //圆角设置,给水波纹也设置同样的圆角
                                  //如果这里不设置就会出现矩形的水波纹效果
                                  borderRadius: new BorderRadius.circular(15.0),
                                  //设置点击事件回调
                                  onTap: () {
                                    // Navigator.of(context).push(
                                    //     new MaterialPageRoute(builder: (_) {
                                    //   return new JIanceDetailPage(
                                    //       order["id"].toString());
                                    // }));
                                    Navigator.of(context).pop(order);
                                  },
                                  child: Container(
                                    //设置 child 居中
                                    alignment: Alignment(0, 0),
                                    height: 30,
                                    width: 85,
                                    child: Text(
                                      "选择订单",
                                      style: TextStyle(
                                          color:
                                              Color.fromRGBO(27, 182, 251, 1)),
                                    ),
                                  )),
                            ),
                          ),
                        )
                        // child: new RaisedButton(
                        //   child: Text('申请补考'),
                        //   onPressed: () {},
                        // ),
                        ),
                  ),
                ],
              ),
            )));
      }
    }
    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  // Widget buildcontent() {
  //   List<Widget> tiles = [];
  //   Widget content;
  //   if (formList.length > 0) {
  //     for (var item in formList) {
  //       tiles.add(Padding(
  //           padding: new EdgeInsets.fromLTRB(15, 15, 15, 0),
  //           child: Container(
  //             decoration: new BoxDecoration(
  //               color: Colors.white,
  //             ),
  //             child: new GestureDetector(
  //                 behavior: HitTestBehavior.opaque,
  //                 onTap: () async {
  //                   Navigator.of(context)
  //                       .push(new MaterialPageRoute(builder: (_) {
  //                     return new TaskdetailPage(item['id']);
  //                   }));
  //                 },
  //                 child: Column(
  //                   mainAxisAlignment: MainAxisAlignment.start,
  //                   crossAxisAlignment: CrossAxisAlignment.start,
  //                   children: <Widget>[
  //                     Padding(
  //                       padding: new EdgeInsets.fromLTRB(20, 15, 20, 10),
  //                       child: Column(
  //                         children: <Widget>[Text(item["title"])],
  //                       ),
  //                     ),
  //                     Padding(
  //                       padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
  //                       child: Column(
  //                         children: <Widget>[
  //                           Text('任务需求：' + item["require_data_des"],
  //                               style: TextStyle(fontSize: 13))
  //                         ],
  //                       ),
  //                     ),
  //                     Padding(
  //                       padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
  //                       child: Column(
  //                         children: <Widget>[
  //                           Text(
  //                             '任务奖励：' + item["reward_data_des"],
  //                             style: TextStyle(fontSize: 13),
  //                           )
  //                         ],
  //                       ),
  //                     ),
  //                     // Padding(
  //                     //   padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
  //                     //   child: Row(
  //                     //     children: <Widget>[
  //                     //       Text(
  //                     //         '资料类型：' + item["type_str"],
  //                     //         style: TextStyle(fontSize: 13),
  //                     //       ),
  //                     //       Padding(
  //                     //           padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
  //                     //           child: Image.asset(
  //                     //               item['cancheck'] == "1"
  //                     //                   ? 'resource/images/user/unlock.png'
  //                     //                   : 'resource/images/user/lock.png',
  //                     //               width: 20,
  //                     //               height: 15)),
  //                     //     ],
  //                     //   ),
  //                     // ),
  //                     // item['image_url'].length > 0
  //                     //     ? Padding(
  //                     //         padding: new EdgeInsets.fromLTRB(5, 10, 20, 10),
  //                     //         child: buildImgs(item),
  //                     //         // child: Row(
  //                     //         //   children: <Widget>[Text('总试题数：16')],
  //                     //         // ),
  //                     //       )
  //                     //     : new Container(height: 0),
  //                     Padding(
  //                       padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
  //                       child: Row(
  //                         children: <Widget>[
  //                           Expanded(
  //                               flex: 1,
  //                               child: Text(
  //                                 item["starttime"],
  //                                 style: TextStyle(
  //                                     color: Color.fromRGBO(153, 153, 153, 1),
  //                                     fontSize: 13),
  //                               )),
  //                         ],
  //                       ),
  //                     ),
  //                   ],
  //                 )),
  //           )));
  //     }
  //   }
  //   content = new Column(
  //       children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
  //       //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
  //       );
  //   return content;
  // }

 

  Widget build(BuildContext context) {
    return
        // new MaterialApp(
        //     title: '结构件',
        //     home:
        new Scaffold(
            appBar: new AppBar(
              title: new Text(
                '关联订单',
                style: TextStyle(color: Colors.black),
              ),
              backgroundColor: Colors.white,
              elevation: 0,
              // backgroundColor: Color.fromRGBO(255, 204, 48, 1),
            ),
          
            body: ModalProgressHUD(
                // color: Colors.blue,
                inAsyncCall: _saving,
                child: new Center(
                    child: Container(
                        decoration: new BoxDecoration(
                          color: Color.fromRGBO(238, 238, 238, 1),
                        ),
                        child:
                            // new ListView(children: <Widget>[
                            Stack(
                          children: <Widget>[
//                             Align(
//                               alignment: Alignment.topLeft,
//                               child: SizedBox(
//                                   width: double.infinity,
//                                   height: 60,
//                                   child: Padding(
//                                       padding: new EdgeInsets.fromLTRB(
//                                           15, 10, 15, 10),
//                                       // padding: new EdgeInsets.all(10),
//                                       child: Container(
//                                           child: Row(
//                                               mainAxisAlignment:
//                                                   MainAxisAlignment.end,
//                                               children: <Widget>[
//                                             Expanded(
//                                               child: new Container(
//                                                 child: new TextField(
//                                                   onSubmitted: (value) {
//                                                     print(value);
//                                                     //if(value=="")return;
//                                                     setState(() {
//                                                       keyword = value;
//                                                       page = 1;
//                                                       end = false;
//                                                     });
//                                                     print('搜索');
//                                                     loadData();
//                                                   },
//                                                   // controller: _phonecontroller,
//                                                   decoration:
//                                                       new InputDecoration(
//                                                           border:
//                                                               InputBorder.none,
//                                                           hintText: '搜索',
//                                                           prefixIcon: Icon(
//                                                               Icons.search)),
//                                                 ),
//                                                 decoration: new BoxDecoration(
// //背景
//                                                   color: Colors.white,
//                                                   //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
//                                                   borderRadius:
//                                                       BorderRadius.all(
//                                                           Radius.circular(
//                                                               25.0)),
//                                                   //设置四周边框
//                                                   border: new Border.all(
//                                                       width: 1,
//                                                       color: Colors.white),
//                                                 ),
//                                               ),
//                                             ),
//                                             // FlatButton(
//                                             //     onPressed: () {
//                                             //       Navigator.pushNamed(context, "upAsk");
//                                             //     },
//                                             //     child: new Text(
//                                             //       "我要提问",
//                                             //       style: TextStyle(
//                                             //           color: Color.fromRGBO(
//                                             //               27, 182, 251, 1),
//                                             //           fontSize: 13),
//                                             //     ))
//                                           ])))),
//                             ),
                            Align(
                                alignment: Alignment.bottomCenter,
                                child: Padding(
                                    padding:
                                        new EdgeInsets.fromLTRB(0, 10, 0, 10),
                                    child: new RefreshIndicator(
                                      onRefresh: _onRefresh,
                                      child: formList.length == 0 && page == 1
                                          ? Nodata()
                                          : new ListView(
                                              controller: _controller,
                                              children: <Widget>[
                                                  buildcontent()
                                                ]),
                                    ))),
                            // Align(
                            //     alignment: Alignment.bottomCenter,
                            //     child: Padding(
                            //         padding:
                            //             new EdgeInsets.fromLTRB(15, 60, 15, 10),
                            //         child: PrimayButton(
                            //            containercolor: Color.fromRGBO(238, 238, 238, 1),
                            //           text: '我的任务',
                            //           width: double.infinity - 30,
                            //           height: 40,
                            //           textcolor: Colors.white,
                            //           bordercolor: Colors.blue,
                            //           colors: Colors.blue,
                            //           borderwidth: 1,
                            //           onPressed: () {
                            //             //  Navigator.of(context).push(
                            //             //     new MaterialPageRoute(builder: (_) {
                            //             //   return new PickVideo();
                            //             // }));
                            //             Navigator.of(context).push(
                            //                 new MaterialPageRoute(builder: (_) {
                            //               return new MyTaskPage();
                            //             }));
                            //           },
                            //         )))
                          ],
                        )))
                //])))),
                ));
  }

  Future<Null> _onRefresh() async {
    await Future.delayed(Duration(seconds: 3), () {
      print('refresh');
      setState(() {
        page = 1;
      });
      loadData();
    });
  }
}
